package com.example.pinkcat.activity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.pinkcat.R;
import com.example.pinkcat.activity.api.entity.TvListEntity;

import java.util.ArrayList;
import java.util.List;


public class VP1_recyleview_adapter_underrecycle extends RecyclerView.Adapter<VP1_recyleview_adapter_underrecycle.ViewHolder>{
    private final Context context;
    int pl_numbers;
    int now_location;
    RecyclerView recyclerView;
    List<TvListEntity> tvListEntities=new ArrayList<>();
    VideoView videoView;

    //构造方法
    public VP1_recyleview_adapter_underrecycle(Context context, int pl_numbers, RecyclerView recyclerView, VideoView videoView){
        this.context=context;
        this.pl_numbers=pl_numbers;
        this.now_location=0;
        this.recyclerView=recyclerView;
        this.videoView=videoView;
    }


    public void setTvlist(List<TvListEntity> tvListEntities){
        this.tvListEntities=tvListEntities;
        notifyDataSetChanged();
    }
    //内部内
    public static class ViewHolder extends RecyclerView.ViewHolder{
        View xunjiback;
        TextView numji;//第几集
        TextView numjiname;//集标题

        //寻找声明布局控件
        public ViewHolder(View view) {
            super(view);
            xunjiback=view.findViewById(R.id.xuajiback);
            numji=view.findViewById(R.id.num_ji);
            numjiname=view.findViewById(R.id.num_jiname);
        }
    }
    //创建获取 view  布局
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //获取item内容
        View view;
            view= LayoutInflater.from(context).inflate(R.layout.list_xuanji,parent,false);
        return new ViewHolder(view);
    }

    //绑定数据
    @SuppressLint({"CheckResult", "SetTextI18n"})
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //初始化item
        holder.numji.setText("第 "+(position+1)+" 集");
        holder.xunjiback.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_grey));
        holder.numji.setTextColor(context.getResources().getColor(R.color.black));
        holder.numjiname.setTextColor(context.getResources().getColor(R.color.black));
        //初始化item
        System.out.println(position+"loc:"+now_location);
        if (position==now_location){
            System.out.println(position+"be pinked:");
            holder.xunjiback.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_pink));
            holder.numji.setTextColor(context.getResources().getColor(R.color.pink));
            holder.numjiname.setTextColor(context.getResources().getColor(R.color.pink));
        }
        System.out.println(tvListEntities.get(position).toString()+"视频集数数据");
        if (tvListEntities.size()>0){
            holder.numjiname.setText(tvListEntities.get(position).getVideo_name());
            holder.xunjiback.setOnClickListener(new View.OnClickListener() {//选集刷新视频连接
                @Override
                public void onClick(View v) {
                    now_location=position;
                    Toast.makeText(context,"播放第 "+(position+1)+"集",Toast.LENGTH_SHORT).show();
                    System.out.println(Uri.parse(tvListEntities.get(position).getVideo_like()));
                    videoView.setVideoURI(Uri.parse(tvListEntities.get(position).getVideo_like()));
                    videoView.start();
                    notifyDataSetChanged();
                    recyclerView.scrollToPosition(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return tvListEntities.size();
    }
}
